/**
 * ctimer.h
 *
 *  Created on: 2017-4-8
 *      Author: chenshisheng
 */

#ifndef CTIMER_H_
#define CTIMER_H_

#include "main.h"

BEGIN_C_DECLS
#ifdef CMSIS_OS
#   include "FreeRTOSConfig.h"

#   define CTIMER_TICK_CLK configTICK_RATE_HZ
#else
#   define CTIMER_TICK_CLK 100
#endif

#define CTIMER_FLAG_ONCE      (0x01 << 3)
#define CTIMER_FLAG_REALTIME  (0x01 << 4)
// 在启动时触发回调
#define CTIMER_FLAG_FIRE_ON_START (0x01 << 5)
#define CTIMER_FLAG_PAUSE     (0x01 << 6)

#define CTIMER_TICK_MIN       (1000 / CTIMER_TICK_CLK)
#define CTIMER_TICK_MS(ms)    ms
#define CTIMER_TICK_SECOND(s) (s * 1000)
#define CTIMER_TICK_MINUTE(m) (m * CTIMER_TICK_SECOND(60))
#define CTIMER_TICK_HOUR(h)   (h * CTIMER_TICK_MINUTE(60))

typedef struct Ctimer
{
#ifndef CMSIS_OS
    struct Ctimer *next; // for list
    unsigned int counter;
    unsigned int interval;
    unsigned int flags;
    void *arg;
    Callback_t callback;
#else
    StaticTimer_t _st;
    osTimerId_t id;
    unsigned int interval;
    unsigned int flags;
#endif
}Ctimer_t;

void Ctimer_Init(void);
int Ctimer_Start(Ctimer_t *timer,
        unsigned int intervalMs,
        Callback_t cb,
        unsigned int flags,
        void *arg);
int Ctimer_Restart(Ctimer_t *timer, unsigned int intervalMs);
int Ctimer_Reset(Ctimer_t *timer);
int Ctimer_Stop(Ctimer_t *timer);
int Ctimer_IsStarted(const Ctimer_t *timer);
int Ctimer_Pause(Ctimer_t *timer, bool isPause);
bool Ctimer_IsPaused(const Ctimer_t *timer);
bool Ctimer_IsActive(const Ctimer_t *timer);
void Ctimer_Service(void);

END_C_DECLS
#endif /* CTIMER_H_ */
